#include "ClientManager.hpp"

/**
 * add a client in the clientManager
 * @param client*
 **/
int ClientManager::add(Client* client)
{
	// TODO : test if client already exists
	// TODO : locks
	this->clientByLogin[client->login]=client;
	this->clientListByMap[client->map][client->login]=client;
	this->clientBySfd[client->sfd]=client;
	return 0;
}

/**
 * remove a client from the clientManager
 * @param client*
 **/
int ClientManager::remove(Client* client)
{
	// TODO : locks
	this->clientByLogin.erase(client->login);
	this->clientListByMap[client->map].erase(client->login);
	this->clientBySfd.erase(client->sfd);
	return 0;
}

/**
 * change map from the client login
 * @param newMap, login
 * @return 0 : OK | -1 : error
 **/
int ClientManager::changeMap(std::string newMap,std::string login)
{
	// TODO : locks
	Client* client=this->clientByLogin[login];

	/* test if the client exists */
	if(client->sfd==-1)
	{
		this->clientByLogin.erase(login);
		return -1;
	}

	/* remove from old map */
	this->clientListByMap[client->map].erase(client->login);

	/* add in new map */
	this->clientListByMap[newMap].erase(client->login);

	/* done */
	return 0;
}

/**
 * get a client from its login 
 * @param login
 * @return client* | NULL
 **/
Client* ClientManager::getClientByLogin(std::string login)
{
	Client* client=this->clientByLogin[login];

	/* test if the client exists */
	if(client->sfd==-1)
	{
		this->clientByLogin.erase(login);
		return NULL;
	}

	return client;
}

/**
 * get a client from its sfd 
 * @param sfd 
 * @return client* | NULL
 **/
Client* ClientManager::getClientBySfd(int sfd)
{
	Client* client=this->clientBySfd[sfd];

	/* test if the client exists */
	if(client->sfd==-1)
	{
		this->clientBySfd.erase(sfd);
		return NULL;
	}

	return client;
}

/**
 * get a client map<login> from a map name
 * @param map
 * @return map<string,Client*>
 **/
std::map<std::string,Client*>* ClientManager::getClientsByMap(std::string map)
{
	return &this->clientListByMap[map];
}

/**
 * constructor
 **/
ClientManager::ClientManager()
{
	// TODO
}

/**
 * destructor
 **/
ClientManager::~ClientManager()
{
	// TODO
}
